<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">添加文章</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="文章标题">
          <el-input v-model="form['title']"/>
        </el-form-item>
        <el-form-item label="权重">
          <el-input-number v-model="form['weight']" :min="1" :max="1000"/>
        </el-form-item>
        <el-form-item label="所属栏目" maxlength="20">
          <el-select v-model="form['typeId']" placeholder="请选择">
            <el-option v-for="item in articleTypeList" :key="item.id" :value="item['id']" :label="item['name']"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容">
          <ws-editor v-model="form['content']"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import WsEditor from 'vue-element-simple/components/UEditor/index';
  export default {
    name: 'InstructionsSublistAdd',
    components: {WsEditor},
    data() {
      return {
        loading: false,
        form: {
          // 文章标题
          title: '',
          // 所属栏目
          typeId: null,
          // 序号
          weight: '',
          // 文章内容
          content: ''
        },
        // 所有栏目列表
        articleTypeList: []
      }
    },
    mounted(){
      this.form['typeId'] = (this.$router.currentRoute.query['typeId'] && parseInt(this.$router.currentRoute.query['typeId'])) || '';
      this.loadArticleTypeList();
    },
    methods: {
      // 加载所有栏目列表
      loadArticleTypeList() {
        (async () => {
          const result = await this.$service.applicationGetAllArticleTypeList();
          if (result.code === 0) {
            this.articleTypeList = result.data
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              const result = await this.$service.applicationAddArticle(params);
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>
